package SiTuQingYou.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * Servlet implementation class CheckCodeServlet
 */
@WebServlet("/CheckCodeServlet")
public class CheckCodeServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckCodeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	

	//������֤��
	protected void createCheckCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int height = 30;
		int width = 100;
		
		response.setContentType("image/jpeg");
		
		String checkCode = getMixCode(4);
		request.getSession().setAttribute("checkCode", checkCode);
		
		Font font = new Font("΢���ź�",Font.PLAIN,20);
		
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics g = bi.getGraphics();
		g.setFont(font);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);
		/*11-02 23:33*/
		for (int i = 0; i < checkCode.length(); i++) 
		{
			char t = checkCode.charAt(i);
			g.setColor(getRandomColor());
			g.drawString(t+"", 10+25*i, 25);
		}
		
		for (int i = 0 , n = getNumber(100); i < n; i++)
		{
			g.setColor(getRandomColor());
			g.drawRect(getNumber(width), getNumber(height), 1, 1);
		}
		
		ServletOutputStream out = response.getOutputStream();
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(bi);
        out.flush();
	}
	
	//�ж��������֤���Ƿ���ȷ
	protected void check(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//����������֤��
		String inputcode = request.getParameter("inputcode");
		
		//������ɵ���֤��
		HttpSession session = request.getSession();
		String code = (String)session.getAttribute("checkCode");
		
		//����������֤��ֱ�ת���ɴ�д�����ڱȽ�
		inputcode = inputcode.toUpperCase();
		code = code.toUpperCase();
		PrintWriter out = response.getWriter();
		if(inputcode.equals(code))
		{
			out.print("yes");
		}
		else
		{
			out.print("no");
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stubr
		
	}
	
	
	
	
	
	private Color getRandomColor()
			{
				Random random = new Random();
				int r = random.nextInt(256);
				int g = random.nextInt(256);
				int b = random.nextInt(256);
				return new Color(r, g, b);
			}
			
		    
	private char getUpper()
			{
				try {
					Random random = new Random();
					return (char) (random.nextInt(26)+65);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 'G';
			}
			
		    
	private char getLower()
			{
				try {
					Random random = new Random();
					return (char) (random.nextInt(26)+97);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 's';
			}
			
		    
	private int getNumber()
			{
				try {
					Random random = new Random();
					return random.nextInt(10);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 6;
			}
			
		    
	private int getNumber(Integer limit)
			{
				try {
					Random random = new Random();
					return random.nextInt(limit);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 6;
			}
			
		    
	public String getMixCode(Integer length)
			{
				StringBuffer sb = new StringBuffer();
				Random random = new Random();
				if (length!=null) 
				{
					int r = 0;
					for (int i = 0; i < length; i++) {
						r = random.nextInt(3);
						switch (r) {
						case 0:
							sb.append(getUpper());
							break;
						case 1:
							sb.append(getLower());
							break;
						case 2:
							sb.append(getNumber());
							break;
						}
					}
					return sb.toString();
				}
				else
				{
					return null;
				}
			}
			
		    
	public String getNumberCode(Integer length)
			{
				StringBuffer sb = new StringBuffer();
				if (length!=null)
				{
					for (int i = 0; i < length; i++) 
					{
						sb.append(getNumber());
					}
					return sb.toString();
				}
				else
				{
					return null;
				}
			}
	

}
